const fs = require('fs');
const path = require('path');

function resolve(dir) {
    return path.resolve(__dirname, dir)
}
const join = path.join

function getEntries(path) {
    let files = fs.readdirSync(resolve(path));
    const entries = files.reduce((ret, item) => {
        const itemPath = join(path, item)
        const isDir = fs.statSync(itemPath).isDirectory();
        // console.log(itemPath)
        if (itemPath != 'packages\\assets' && itemPath != 'packages\\utils' && itemPath != 'packages/assets' && itemPath != 'packages/utils') { // 是assets公共样式不被打包
            if (isDir) {
                ret[item] = resolve(join(itemPath, 'index.js'))
            } else {
                const [name] = item.split('.')
                ret[name] = resolve(`${itemPath}`)
            }
        }
        return ret
    }, {})
    return entries
}
//开发环境配置
const devConfig = {
        'runtimeCompiler': true,
        // 'transpileDependencies': [/[/\\]node_modules[/\\](.+?)?exceljs(.*)/, /[/\\]node_modules[/\\](.+?)?js-base64(.*)/],
        pages: {
            index: {
                entry: 'examples/main.js',
                template: 'public/index.html',
                filename: 'index.html',
            },
        },
        configureWebpack: {
            // externals: {
            //     './cptable': 'var cptable',
            //     '../xlsx.js': 'var _XLSX'
            // },
            resolve: {
                extensions: ['.js', '.vue', '.json'],
                alias: {
                    '@': resolve('packages'),
                    'assets': resolve('examples/assets'),
                    'views': resolve('examples/views'),
                }
            },
            devtool: 'source-map',

        },
        chainWebpack: config => {
            // config.module
            //     .rule('js')
            //     .include
            //     .add('/packages')
            //     .end()
            //     .use('babel-loader')
            //     .loader('babel-loader')
            //     .options({
            //         babelrc: false,
            //         plugins: ["@vue/cli-plugin-babel/preset"],
            //         presets: [require.resolve('@vue/cli-plugin-babel/preset')]
            //     });
            // .presets(['@vue/cli-plugin-babel/preset'])
            // .tap(options => {
            //     options.exclude = /a.umd/
            //     console.log('##', options)
            //     return options
            // })
        },
        devServer: {
            port: 8091,
            hot: true,
            open: 'Google Chrome',
            'disableHostCheck': true,
            'historyApiFallback': true,
            'proxy': {
                '/ts-system': {
                    'target': 'http://192.168.18.228:9099',
                    'changeOrigin': true,
                    'secure': false,
                    'pathRewrite': {
                        '^/ts-system': '/ts-system'
                    }
                },


                '/ts-portal': {
                    'target': 'http://192.168.18.228:9099',
                    'changeOrigin': true,
                    'secure': false,
                    'pathRewrite': {
                        '^/ts-portal': '/ts-portal'
                    }
                },
                '/ts-bi': {
                    'target': 'http://192.168.18.228:9099',
                    'changeOrigin': true,
                    'secure': false,
                    'pathRewrite': {
                        '^/ts-system': '/ts-bi'
                    }
                }
            }
        }
    }
    //生成环境配置
const buildConfig = {
    // 'transpileDependencies': [/[/\\]node_modules[/\\](.+?)?exceljs(.*)/, /[/\\]node_modules[/\\](.+?)?crypto-js(.*)/],
    css: {
        sourceMap: true,
        extract: {
            filename: 'style/[name].css'
        }
    },
    configureWebpack: {
        entry: {
            ...getEntries('packages'),
        },
        output: {
            filename: '[name]/index.js',
            libraryTarget: 'commonjs2',
        }
    },
    chainWebpack: config => {
        // config.module
        //     .rule('js')
        //     .include
        //     .add('/packages')
        //     .end()
        //     .use('babel')
        //     .loader('babel-loader')
        //     .options({
        //         presets: ['@vue/cli-plugin-babel/preset']
        //     });
        config.optimization.delete('splitChunks')
        config.plugins.delete('copy')
        config.plugins.delete('html')
        config.plugins.delete('preload')
        config.plugins.delete('prefetch')
        config.plugins.delete('hmr')
        config.entryPoints.delete('app')

        config.module
            .rule('fonts')
            .use('url-loader')
            .tap(option => {
                option.fallback.options.name = 'static/fonts/[name].[hash:8].[ext]'
                return option
            })
    },
    outputDir: 'lib',
    productionSourceMap: false,
}

module.exports = process.env.NODE_ENV === 'development' ? devConfig : buildConfig;